package com.css.apps.base.func.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.func.model.SFunc;
import com.css.apps.base.func.model.SFuncAction;
import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.role.model.SRoleFunc;
import com.css.core.configuration.Environment;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

/**
 * Copyright (c) Css Team All rights reserved.
 *
 * This file DelFuncAction.java creation date: [Jan 9, 2014 1:46:27 PM] by
 * Xingzhc http://www.css.com.cn
 */
@SuppressWarnings("serial")
public class DelFunc extends AdminAction {
	private static Log log = LogFactory.getLog(DelFunc.class);
	private String ids;

	@SuppressWarnings("unchecked")
	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			if (StringHelper.isEmpty(ids)) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			List<String> listIds = StringHelper.strToList(ids);
			List<String> delList = new ArrayList<String>();
			List<SFunc> funcList = QueryCache.idToObj(SFunc.class, listIds);
			int num = 0;
			tx = new TransactionCache();

			Tree tree = new TreeCache(SFunc.class);
			for (SFunc func : funcList) {
				List<SFuncAction> funcActionlist = FuncPointItem.getFuncActionByCode(func.getUuid());
				List<SRoleFunc> roleFuncList = SRoleFunc.getRoleFuncByfuncId(func.getUuid());
				TreeNode funcNode = tree.getTreeNode(func.getUuid());
				List<TreeNode> childrens = funcNode.getAllChildren();
				for (TreeNode node : childrens) {
					SFunc func_sub = QueryCache.get(SFunc.class, node.getNodeId());
					List<SFuncAction> funcActionlist_sub = FuncPointItem.getFuncActionByCode(func_sub.getUuid());
					List<SRoleFunc> roleFuncList1_sub = SRoleFunc.getRoleFuncByfuncId(func_sub.getUuid());
					delList.add(func.getUuid().trim());
					tx.delete(func_sub);
					tx.delete(funcActionlist_sub);
					tx.delete(roleFuncList1_sub);
					// 记录删除功能，角色功能关联，功能项日志
					LogPart lp = new LogPart();
					lp.setOpObjType(SFunc.class.getName());
					lp.setOpObjId(func_sub.getUuid());
					lp.setRelObjType(SFunc.class.getName());
					lp.setRelObjId(func_sub.getUuid());
					lp.setOpType(LogConstant.LOG_TYPE_DELETE);
					lp.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
					lp.setLogData("");
					lp.setResult(LogConstant.RESULT_SUCCESS);
					lp.save();
					for (SRoleFunc rolefunc : roleFuncList1_sub) {
						LogPart lprolefunc = new LogPart();
						lprolefunc.setOpObjType(SRoleFunc.class.getName());
						lprolefunc.setOpObjId(rolefunc.getUuid());
						lprolefunc.setRelObjType(SFunc.class.getName());
						lprolefunc.setRelObjId(func_sub.getUuid());
						lprolefunc.setOpType(LogConstant.LOG_TYPE_DELETE);
						lprolefunc.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
						lprolefunc.setLogData("");
						lp.setResult(LogConstant.RESULT_SUCCESS);
						lprolefunc.save();
					}
					for (SFuncAction funcaAtion : funcActionlist_sub) {
						LogPart lprolefunc = new LogPart();
						lprolefunc.setOpObjType(SFuncAction.class.getName());
						lprolefunc.setOpObjId(funcaAtion.getUuid());
						lprolefunc.setRelObjType(SFunc.class.getName());
						lprolefunc.setRelObjId(func_sub.getUuid());
						lprolefunc.setOpType(LogConstant.LOG_TYPE_DELETE);
						lprolefunc.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
						lprolefunc.setLogData("");
						lp.setResult(LogConstant.RESULT_SUCCESS);
						lprolefunc.save();
					}
				}
				delList.add(func.getUuid().trim());
				tx.delete(func);
				tx.delete(roleFuncList);
				tx.delete(funcActionlist);

				LogPart lp = new LogPart();
				lp.setOpObjType(SFunc.class.getName());
				lp.setOpObjId(func.getUuid());
				lp.setRelObjType(SFunc.class.getName());
				lp.setRelObjId(func.getUuid());
				lp.setOpType(LogConstant.LOG_TYPE_DELETE);
				lp.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
				lp.setLogData("");
				lp.setResult(LogConstant.RESULT_SUCCESS);
				lp.save();

				for (SRoleFunc rolefunc : roleFuncList) {
					LogPart lprolefunc = new LogPart();
					lprolefunc.setOpObjType(SRoleFunc.class.getName());
					lprolefunc.setOpObjId(rolefunc.getUuid());
					lprolefunc.setRelObjType(SFunc.class.getName());
					lprolefunc.setRelObjId(func.getUuid());
					lprolefunc.setOpType(LogConstant.LOG_TYPE_DELETE);
					lprolefunc.setLogLevel(LogConstant.LOG_LEVEL_IMPORTANT);
					lprolefunc.setLogData("");
					lp.setResult(LogConstant.RESULT_SUCCESS);
					lprolefunc.save();
				}
			}
			tx.commit();
			tree.refresh();
			if (num > 0) {
				String str = Integer.toString(num);
				result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS,
						Messages.getString("systemMsg.NoDeleteFuncCount", new String[] { str }), delList);
				return Action.SUCCESS;

			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"), listIds);
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

}
